/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: future.h
 *
 * Purpose: implementation future functional
 *
 * Developer:
 *   wen.gu , 2022-08-25
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __DOG_CORE_FUTURE_H__
#define __DOG_CORE_FUTURE_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <exception>
#include "dog/core/result.h"
#include "dog/core/error_code.h"
#include "dog/core/internal/future_shared_state.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace dog {
namespace core {
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

template <typename T, typename E = ErrorCode>
class Future;

template <typename U>
struct AddFuture {
    using type = Future<U, ErrorCode>;
};

template <typename T, typename E>
struct AddFuture<Future<T, E>> {
    using type = Future<T, E>;
};

template <typename T, typename E>
struct AddFuture<Result<T, E>> {
    using type = Future<T, E>;
};

template <typename U>
using AddFuture_t = typename add_future<T>::type;



template<typename T, typename E = ErrorCode>
class Future final {
public:
    Future() noexcept = default;
    Future(Future &&other) noexcept;
    ~Future() noexcept;
    Future& operator=(Future &&other) noexcept;

public:
    Future(const Future &) = delete;
    Future& operator=(const Future &) = delete;

public:
    T get ();
    Result<T, E> get_result () noexcept;

public:
    bool valid() const noexcept;
    void wait() const;

    template <typename Rep, typename Period>
    FutureStatus wait_for(const std::chrono::duration<Rep, Period>& timeoutDuration) const;

    template <typename Clock, typename Duration>
    FutureStatus wait_until(const std::chrono::time_point<Clock, Duration> &deadline) const;

public:
    template <typename F>
    auto then (F &&func) -> Future< <see below> >;

    template <typename F, typename ExecutorT>
    auto then (F &&func, ExecutorT &&executor) -> Future< <see below> >;

};

} /** namespace core */
} /** namespace dog */
#endif /** !__DOG_CORE_FUTURE_H__ */

